package com.qc31.baselib.utils

import android.content.Context
import android.content.res.Configuration
import android.content.res.Resources
import android.os.Build
import android.os.LocaleList
import java.util.*

/** @Author Jasper @Time 2021/11/18 14:40 @Desc */
class Language private constructor() {
    private val languageMap = HashMap<String, Locale>()
    var language: String = ""

    init {
        languageMap["zh_CN"] = Locale.CHINA
        languageMap["zh_TW"] = Locale.TAIWAN
        languageMap["en_US"] = Locale.ENGLISH
    }

    companion object {
        val instance by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
            Language().apply { initLanguage() }
        }
    }

    fun isEnglish(): Boolean = language == "en_US"

    /** 初始化判断语言 */
    fun initLanguage() {

        val locale = getDefaultLanguage()
        language = if (locale.language == "zh") {
            if (locale.country == "CN") "zh_CN" else "zh_TW"
        } else {
            "en_US"
        }

        MLog.e("语言     $language")
    }

    /** 获取系统默认语言 */
    private fun getDefaultLanguage(): Locale {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            LocaleList.getDefault().get(0)
        } else {
            Locale.getDefault()
        }
    }

    /** 检测语言是支持的类型 仅适用于此处 */
    private fun isSupportLanguage(locale: Locale): Boolean {
        return locale.language == "zh" || locale.language == "en"
    }

    /** 获取语言Locale */
    private fun getSupportLanguage(): Locale {
        MLog.e("语言------${!BuildConfigs.ISCOMPANY} $language ${languageMap[language]}")
        val locale = if (!BuildConfigs.ISCOMPANY) languageMap[language]
            ?: getDefaultLanguage() else getDefaultLanguage()
        return if (isSupportLanguage(locale)) {
            locale
        } else {
            Locale.ENGLISH
        }
    }

    /** 设置语言Context */
    fun attachBaseContext(context: Context): Context {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            createResource(context)
        } else {
            updateResource(context)
        }
    }

    /** 7.0之后设置语言 */
    private fun createResource(context: Context): Context {
        val resources: Resources = context.resources
        val configuration: Configuration = resources.configuration
        val targetLocale: Locale = getSupportLanguage()
        MLog.e("语言设置7.0之后====== $targetLocale")
        configuration.setLocale(targetLocale)
        resources.updateConfiguration(configuration, resources.displayMetrics)
        //创建配置
        return context.createConfigurationContext(configuration)
    }

    /** 7.0之前设置语言 */
    private fun updateResource(context: Context): Context {
        val targetLocale: Locale = getSupportLanguage()
        val configuration: Configuration = context.resources.configuration
        MLog.e("语言设置====== $targetLocale")
        configuration.setLocale(targetLocale)
        val resources: Resources = context.resources
        //更新配置
        resources.updateConfiguration(configuration, resources.displayMetrics)
        return context
    }


}